package com.yunhe.abnormal.repository.device.impl;

import com.yunhe.abnormal.domain.base.AlarmStatus;
import org.neo4j.ogm.model.Result;
import org.neo4j.ogm.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public class AlarmStatusDeviceRepositoryDao {

    @Autowired
    private Session session;

    /**
     * 执行语句搜索设备告警条件
     *
     * @param cypher
     * @return
     */
    public List<Map<String, Object>> search(String cypher) {
        Result result = session.query(cypher, new HashMap<>());
        Iterator<Map<String, Object>> iterator = result.iterator();
        List<Map<String, Object>> list = new ArrayList<>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }

    /**
     * 得到排序规则
     *
     * @param sortList 排序字段列表
     * @return
     */
    private String getSort(List<String> sortList) {
        String sort = " order by ";
        for (String s : sortList) {
            if (s.startsWith("-")) {
                s = s.replace("-", "");
                sort += "a." + s + " DESC,";
            } else {
                sort += "a." + s + ",";
            }
        }
        sort = sort.substring(0, sort.length() - 1);
        return sort;
    }

    /**
     * 得到分页条件
     *
     * @param page 页数
     * @param size 每页条数
     * @return
     */
    private String getPage(int page, int size) {
        return " skip " + page * size + " limit " + size;
    }

    public Map<String, Object> getAlarmStatus(List<Long> stationIds,
                                              String queryStr,
                                              int page,
                                              int size,
                                              List<String> sortList) {
        Map<String, Object> map = new HashMap<>();
        String cql = "match (c:AbnormalType)<-[]-(a:AlarmStatus)-[]->(b:AlarmLevel) with a ";
        if (stationIds != null) {
            map.put("stationIds", stationIds);
            cql += " match (a) where a.stationId in {stationIds} with a";
        }
        if (queryStr != null) {
            map.put("queryStr", queryStr);
            cql += " match (c:AbnormalType)<-[]-(a)-[]->(b:AlarmLevel) where a.title contains {queryStr} or a.devTitle contains {queryStr} " +
                    " or a.condition contains {queryStr} or b.title contains {queryStr} with a ";
        }
        if (sortList != null) {
            String sort = getSort(sortList);
            cql += sort;
        }
        String countCql = cql + " with distinct a return count (a) ";
        Iterable<Long> counts = session.query(Long.class, countCql, map);
        Long count = 0L;
        if (counts.iterator().hasNext()) {
            count = counts.iterator().next();
        }
        if (page >= 0 && size > 0) {
            String pageStr = getPage(page, size);
            cql += pageStr;
        }
        cql += " match p=(a)-[:ABNORMAL_TYPE_OF|:ALARM_LEVEL_OF|:POINTDATATYPE_OF]->(obj) return p ";
        Iterable<AlarmStatus> alarmStatuses = session.query(AlarmStatus.class, cql, map);
        Map<String, Object> result = new HashMap<>();
        result.put("result", alarmStatuses);
        result.put("count", count);
        return result;
    }
}
